package org.blame.lotterysystem.controller;


import org.blame.lotterysystem.common.pojo.CommonResult;
import org.blame.lotterysystem.common.utils.JacksonUtil;
import org.blame.lotterysystem.controller.param.DrawPrizeParam;
import org.blame.lotterysystem.controller.param.ShowWinningRecordsParam;
import org.blame.lotterysystem.controller.result.ShowWinningRecordsResult;
import org.blame.lotterysystem.service.DrawPrizeService;

import org.blame.lotterysystem.service.dto.ShowWinningRecordsListTDTO;
import org.blame.lotterysystem.service.enums.ActivityPrizeTiersEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RestController
public class DrawPrizeController {

    private static final Logger logger= LoggerFactory.getLogger(DrawPrizeController.class);

    @Autowired
    private DrawPrizeService drawPrizeService;

    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@RequestBody @Validated DrawPrizeParam param){
        logger.info("drawPrize  DrawPrizeParam:{}",param);
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<ShowWinningRecordsResult>> ShowWinningRecords(
            @RequestBody @Validated ShowWinningRecordsParam showWinningRecordsParam){
        logger.info("ShowWinningRecords  showWinningRecordsParam:{}", JacksonUtil.writeValueAsString(showWinningRecordsParam));
        List<ShowWinningRecordsListTDTO> showWinningRecordsDTOList=drawPrizeService.showRecords(showWinningRecordsParam);
        return CommonResult.success(convertShowWinningRecordsResult(showWinningRecordsDTOList));

    }

    private List<ShowWinningRecordsResult> convertShowWinningRecordsResult(
            List<ShowWinningRecordsListTDTO> showWinningRecordsDTOList) {
        if(CollectionUtils.isEmpty(showWinningRecordsDTOList)){
            logger.error("参数列表为空,:{}",JacksonUtil.writeValueAsString(showWinningRecordsDTOList));
            return Arrays.asList();
        }
        return showWinningRecordsDTOList.stream()
                .map(showWinningRecordsListTDTO -> {
                    ShowWinningRecordsResult showWinningRecordsResult=new ShowWinningRecordsResult();
                    showWinningRecordsResult.setWinningTime(showWinningRecordsListTDTO.getWinningTime());
                    showWinningRecordsResult.setPrizeTier(showWinningRecordsListTDTO.getPrizeTier().getMessage());
                    showWinningRecordsResult.setPrizeName(showWinningRecordsListTDTO.getPrizeName());
                    showWinningRecordsResult.setWinnerId(showWinningRecordsListTDTO.getWinnerId());
                    showWinningRecordsResult.setWinnerName(showWinningRecordsListTDTO.getWinnerName());
                    return showWinningRecordsResult;
                }).collect(Collectors.toList());

    }
}
